<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring 事件驱动机制深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            border-bottom: 1px solid #3e3e3e;
        }
        
        .code-block pre {
            margin: 0;
            padding: 60px 24px 24px;
            overflow-x: auto;
        }
        
        .section-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 36px;
            height: 36px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            font-weight: 600;
            margin-right: 12px;
        }
        
        .highlight-text {
            background: linear-gradient(to right, #667eea, #764ba2);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .feature-icon {
            width: 48px;
            height: 48px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 32px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 8px;
            top: 8px;
            width: 16px;
            height: 16px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 24px;
            width: 2px;
            height: calc(100% + 16px);
            background: #e5e7eb;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-24">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                Spring 事件驱动机制
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                深入解析 Spring Framework 的事件发布与监听机制
            </p>
            <div class="flex justify-center space-x-8 text-sm md:text-base">
                <div class="flex items-center">
                    <i class="fas fa-broadcast-tower mr-2"></i>
                    <span>事件发布</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-satellite-dish mr-2"></i>
                    <span>事件监听</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-sync-alt mr-2"></i>
                    <span>异步处理</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12">
        <!-- Overview Section -->
        <section class="mb-16">
            <div class="content-card rounded-2xl shadow-xl p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <span class="section-number">1</span>
                    Spring 事件驱动机制概述
                </h2>
                <p class="text-lg leading-relaxed text-gray-700 mb-8">
                    Spring 的事件驱动机制基于观察者模式，允许应用程序的不同部分对特定事件作出响应。Spring 提供了 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ApplicationEventPublisher</code>、<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ApplicationListener</code> 和 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@EventListener</code> 注解等机制来支持事件驱动的编程模型。
                </p>
                
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                        <div class="feature-icon">
                            <i class="fas fa-paper-plane"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-2">事件发布者</h3>
                        <p class="text-gray-600">用于发布事件，触发相应的业务逻辑处理</p>
                    </div>
                    <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                        <div class="feature-icon">
                            <i class="fas fa-headphones"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-2">事件监听器</h3>
                        <p class="text-gray-600">用于接收和处理特定的事件，实现业务逻辑</p>
                    </div>
                    <div class="bg-gradient-to-br from-green-50 to-teal-50 p-6 rounded-xl">
                        <div class="feature-icon">
                            <i class="fas fa-cube"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-2">事件类型</h3>
                        <p class="text-gray-600">通常为 ApplicationEvent 的子类，承载事件数据</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Event Flow Diagram -->
        <section class="mb-16">
            <div class="content-card rounded-2xl shadow-xl p-8 md:p-12">
                <h3 class="text-2xl font-bold mb-6 text-center">
                    <i class="fas fa-project-diagram mr-3 text-purple-600"></i>
                    事件驱动架构流程图
                </h3>
                <div class="mermaid">
                    graph LR
                        A[事件源] -->|发布事件| B[ApplicationEventPublisher]
                        B -->|广播| C[ApplicationEventMulticaster]
                        C -->|分发| D[监听器1]
                        C -->|分发| E[监听器2]
                        C -->|分发| F[监听器3]
                        D -->|处理| G[业务逻辑1]
                        E -->|处理| H[业务逻辑2]
                        F -->|处理| I[业务逻辑3]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
                        style E fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
                        style F fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </section>

        <!-- Event Publishing Section -->
        <section class="mb-16">
            <div class="content-card rounded-2xl shadow-xl p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <span class="section-number">2</span>
                    事件的发布和监听
                </h2>
                
                <div class="mb-10">
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-rocket mr-3 text-purple-600"></i>
                        2.1 事件发布的流程
                    </h3>
                    <p class="text-lg leading-relaxed text-gray-700 mb-6">
                        当我们发布事件时，通常通过 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ApplicationEventPublisher</code> 来触发事件。<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ApplicationContext</code> 是一个典型的 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ApplicationEventPublisher</code> 的实现类。
                    </p>
                    
                    <div class="code-block">
                        <pre><code class="language-java">public class GenericApplicationContext extends AbstractApplicationContext {

    private final List&lt;ApplicationListener&lt;?&gt;&gt; applicationListeners = new ArrayList&lt;&gt;();

    @Override
    public void publishEvent(Object event) {
        // 发布事件
        this.getApplicationEventMulticaster().multicastEvent((ApplicationEvent) event);
    }
}</code></pre>
                    </div>
                    
                    <div class="mt-6 bg-blue-50 border-l-4 border-blue-500 p-4 rounded">
                        <p class="text-gray-700">
                            <i class="fas fa-info-circle mr-2 text-blue-500"></i>
                            <strong>关键点：</strong>GenericApplicationContext 中的 publishEvent 方法用于将事件发布给注册的监听器，ApplicationEventMulticaster 负责将事件通知所有已注册的监听器。
                        </p>
                    </div>
                </div>

                <div>
                    <h3 class="text-2xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-user-plus mr-3 text-purple-600"></i>
                        2.2 事件监听器的注册
                    </h3>
                    <p class="text-lg leading-relaxed text-gray-700 mb-6">
                        事件监听器通过实现 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ApplicationListener</code> 接口或使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@EventListener</code> 注解来注册。
                    </p>
                    
                    <div class="code-block">
                        <pre><code class="language-java">public class ApplicationEventMulticaster {

    private final List&lt;ApplicationListener&lt;?&gt;&gt; listeners = new ArrayList&lt;&gt;();

    public void multicastEvent(ApplicationEvent event) {
        for (ApplicationListener&lt;?&gt; listener : listeners) {
            // 根据事件类型，触发监听器处理
            if (listener.supportsEventType(event.getClass())) {
                listener.onApplicationEvent(event);
            }
        }
    }

    public void addApplicationListener(ApplicationListener&lt;?&gt; listener) {
        listeners.add(listener);
    }
}</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Event Propagation Timeline -->
        <section class="mb-16">
            <div class="content-card rounded-2xl shadow-xl p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-8 flex items-center">
                    <span class="section-number">3</span>
                    事件的传播过程
                </h2>
                
                <div class="timeline-item">
                    <h4 class="text-xl font-semibold mb-2">事件发布</h4>
                    <p class="text-gray-600">应用程序通过 ApplicationEventPublisher 发布事件</p>
                </div>
                
                <div class="timeline-item">
                    <h4 class="text-xl font-semibold mb-2">事件接收</h4>
                    <p class="text-gray-600">ApplicationEventMulticaster 接收到事件并准备分发</p>
                </div